package com.example.stickyheadergridview;

import java.util.List;

import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.model.Schedules;
import com.anbinh.doctor.utils.LichType;
import com.anbinh.doctor.utils.Utils;
import com.tonicartos.widget.stickygridheaders.StickyGridHeadersSimpleAdapter;

public class StickyGridAdapter extends BaseAdapter implements
		StickyGridHeadersSimpleAdapter {

	private List<GridItem> list;
	private LayoutInflater mInflater;
	private GridView mGridView;
	private Point mPoint = new Point(0, 0);// ������װImageView�Ŀ�͸ߵĶ���

	public StickyGridAdapter(Context context, List<GridItem> list,
			GridView mGridView) {
		this.list = list;
		mInflater = LayoutInflater.from(context);
		this.mGridView = mGridView;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public GridItem getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder mViewHolder;
		if (convertView == null) {
			mViewHolder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_lichhen, parent,
					false);
			mViewHolder.lblTime = (TextView) convertView
					.findViewById(R.id.lblTime);
			mViewHolder.lblStatus = (TextView) convertView
					.findViewById(R.id.lblStatus);
			mViewHolder.viewmain = (View) convertView
					.findViewById(R.id.viewmain);

			convertView.setTag(mViewHolder);

		} else {
			mViewHolder = (ViewHolder) convertView.getTag();
		}
		Schedules item = getItem(position).getSchedules();
		if (item == null)
			return convertView;
		mViewHolder.lblTime.setText(item.getStart_time());
		int status;
		String tmp = item.getStatus();
		if (tmp.isEmpty()) {
			status = Utils.LICH_TRONG;// Lịch khám trống
		} else {
			status = Integer.parseInt(tmp);
		}
		convertView.setVisibility(View.VISIBLE);
		// note: status=(-1: lịch trống, 0:lịch chờ duyệt, 1:đã đặt lịch hẹn,2:
		// đã khám, 4:chờ cập nhật trạng thái, -5: lịch tạm dừng khám)
		mViewHolder.lblStatus.setText(getItem(position).getPosition() + "");
		if (status == LichType.LICHTRONG_MAUXANH.getCode()) {
			// mViewHolder.lblStatus.setBackgroundColor(Color
			// .parseColor("#f9dd10"));
			mViewHolder.lblTime.setTextColor(Color.parseColor("#ffffff"));
			mViewHolder.viewmain
					.setBackgroundResource(R.drawable.bgr_lichhen_maxanh);
		} else if (status == LichType.LICH_CHODUYET_MAUVANG.getCode()) {
			// mViewHolder.lblStatus.setBackgroundColor(Color
			// .parseColor("#ffffff"));
			mViewHolder.lblTime.setTextColor(Color.parseColor("#ffffff"));
			mViewHolder.viewmain
					.setBackgroundResource(R.drawable.bgr_lichhen_mauvang);
		} else if (status == LichType.LICH_DADAT_MAUDO.getCode()) {
			// mViewHolder.lblStatus.setBackgroundResource(R.drawable.ic_dakham);
			mViewHolder.viewmain
					.setBackgroundResource(R.drawable.bgr_lichhen_maudo);
			mViewHolder.lblTime.setTextColor(Color.parseColor("#ffffff"));
			// mViewHolder.lblStatus.setBackgroundColor(Color
			// .parseColor("#f9dd10"));

		} else if (status == LichType.LICH_DAKHAM_MAUTRANG.getCode()) {
			// mViewHolder.lblStatus.setBackgroundResource(R.drawable.ic_baothuc);
			mViewHolder.viewmain
					.setBackgroundResource(R.drawable.bgr_lichhen_mautrang);
			mViewHolder.lblTime.setTextColor(Color.parseColor("#000000"));
			// mViewHolder.lblStatus.setBackgroundColor(Color
			// .parseColor("#f9dd10"));

		} else if (status == LichType.LICH_CHOCAPNHAT_TRANGXAM.getCode()) {
			mViewHolder.viewmain
					.setBackgroundResource(R.drawable.bgr_lichhen_mauxam);
			mViewHolder.lblTime.setTextColor(Color.parseColor("#000000"));
			// mViewHolder.lblStatus.setBackgroundColor(Color
			// .parseColor("#f9dd10"));

		} else if (status == LichType.LICHTAMDUNG_NUOCBIEN.getCode()) {
			mViewHolder.viewmain
					.setBackgroundResource(R.drawable.bgr_lichhen_tamdung);
			// mViewHolder.lblStatus.setBackgroundColor(Color
			// .parseColor("#f9dd10"));
			mViewHolder.lblTime.setTextColor(Color.parseColor("#ffffff"));
		} else {
			convertView.setVisibility(View.GONE);
		}

		return convertView;
	}

	@Override
	public View getHeaderView(int position, View convertView, ViewGroup parent) {
		HeaderViewHolder mHeaderHolder;
		if (convertView == null) {
			mHeaderHolder = new HeaderViewHolder();
			convertView = mInflater.inflate(R.layout.header_lichhen, parent,
					false);
			mHeaderHolder.lblAddress = (TextView) convertView
					.findViewById(R.id.lblAddress);
			mHeaderHolder.lblDocterName = (TextView) convertView
					.findViewById(R.id.lblName);
			convertView.setTag(mHeaderHolder);
		} else {
			mHeaderHolder = (HeaderViewHolder) convertView.getTag();
		}

		mHeaderHolder.lblAddress.setText(list.get(position).getGroupAddress());
		mHeaderHolder.lblDocterName.setText(list.get(position).getDoctorName());

		return convertView;
	}

	public static class ViewHolder {
		public TextView lblTime;
		public TextView lblStatus;
		public View viewmain;
	}

	public static class HeaderViewHolder {
		public TextView lblAddress;
		public TextView lblDocterName;

	}

	@Override
	public long getHeaderId(int position) {
		return list.get(position).getSection();
	}

}
